import gamestate
import pygame, os

class Intro(gamestate.GameState):
    """GameState for the intro to the Main Menu"""
    
    def __init__(self):
        #basic keybinds
        self.keybinds = {'advance': pygame.K_RETURN}
        
        #link actions to appropriate function references
        self._actions = {'advance': self._action_advance}
        
        self._newState = self
        
        self._load_images()
        self._currentImage = 0
        self._holdTime = 1000 * .9
        self._timeOnScreen = 0
        
    def draw(self, screen):
        screen.blit(self._images[self._currentImage], (0, 0))
            
    def enter_state(self):
        self._newState = self
        
    def update(self, etime):
        #cycle through images in _images list based on timer
        self._timeOnScreen += etime
        
        if self._timeOnScreen > self._holdTime:
            self._currentImage = (self._currentImage + 1) % len(self._images)
            self._timeOnScreen -= self._holdTime
        
        return self._newState
    
    def _action_advance(self):
        self._newState = gamestate.GameState.validStates['mainmenu']
        
    def _load_images(self):
        self._images = []
        
        try:
            self._images.append(pygame.image.load(os.path.join('imgs', 'to-cont-text.png')))
            self._images.append(pygame.image.load(os.path.join('imgs', 'to-cont-notext.png')))
        except:
            print('An error occurred while loading images for [Intro gamestate]')
            exit(0)